﻿Imports System.Runtime.Serialization

<DataContract(Name:="DFAScannerEngine", Namespace:="urn:VBF.CompilerModel.Scanner")>
Public Class DFAScannerEngine
    <DataMember(Name:="TransitionTable")>
    Private m_transitionTable As CharIndexTable(Of Integer)()
    <DataMember(Name:="AcceptStateTable")>
    Private m_acceptStateTable As Integer()

    Private m_current As Integer

    Friend Sub New(ByVal transitionTable As CharIndexTable(Of Integer)(), ByVal accetpStateTable As Integer())
        Debug.Assert(transitionTable IsNot Nothing AndAlso transitionTable.Length > 0)
        Debug.Assert(accetpStateTable IsNot Nothing AndAlso accetpStateTable.Length > 0)
        Debug.Assert(accetpStateTable.Length = accetpStateTable.Length)

        m_transitionTable = transitionTable
        m_acceptStateTable = accetpStateTable
        m_current = 1
    End Sub

    Public Sub Reset()
        m_current = 1
    End Sub

    Public ReadOnly Property IsStopped() As Boolean
        Get
            Return m_current = 0
        End Get
    End Property

    Public ReadOnly Property IsAccepted() As Boolean
        Get
            Return m_acceptStateTable(m_current) <> 0
        End Get
    End Property

    Public Sub Input(ByVal c As Char)
        Dim indexTable = m_transitionTable(m_current)
        Dim nextState = indexTable.Match(c)

        Debug.Assert(nextState >= 0 AndAlso nextState < m_transitionTable.Length)

        m_current = nextState
    End Sub

    Public Function GetCurrentTokenTypeIndex() As Integer
        Return m_acceptStateTable(m_current)
    End Function

End Class
